package com.air.common.controller.imp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.common.controller.IBasicAuthController;
import com.air.common.exception.NoSuchUserException;
import com.air.common.exception.WrongPasswordException;
import com.air.common.mo.BasicUserInfo;
import com.air.common.service.IBasicUserInfoService;
import com.air.common.service.ISystemConfigService;
import com.air.common.util.BasicAppConstant;
import com.air.common.util.BasicErrorCode;
import com.air.common.util.WebUtil;
import com.air.common.vo.LoginRequestVO;
import com.air.common.vo.ResponseVO;





@Controller
@RequestMapping("/auth/basic")
public class BasicAuthControllerImp extends BaseControllerImp implements
		IBasicAuthController {
	
	@Autowired
	IBasicUserInfoService userInfoService;
	
	@Autowired
	ISystemConfigService systemConfigService;
	
	@Override
	@RequestMapping("doLogin")
	public ModelAndView doLogin(HttpServletRequest request,
			HttpServletResponse response, LoginRequestVO requestVO)
			throws Exception {
		
		BasicUserInfo user = null;
		ResponseVO responseVO = new ResponseVO();
		String loginPageViewName = systemConfigService.findConfigByKey(BasicAppConstant._SYSCONF_KEY_LOGIN_PAGE).getValue();
		try{
			user = userInfoService.validateUser(requestVO.getLoginName(), requestVO.getPassword());
		}catch(NoSuchUserException noUserEx){
			responseVO.setException(noUserEx);
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(BasicErrorCode._ERRCODE_NO_SUCH_USER);
			responseVO.setViewName(loginPageViewName);
			responseVO.addObject(BasicAppConstant._KEY_LOGIN_RETURN_URL, requestVO.getReturnUrl());
			return parse2Mav(request, responseVO);
		}catch(WrongPasswordException wrongPwdEx){
			responseVO.setException(wrongPwdEx);
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(BasicErrorCode._ERRCODE_WRONG_PASSWORD);
			responseVO.setViewName(loginPageViewName);
			responseVO.addObject(BasicAppConstant._KEY_LOGIN_RETURN_URL, requestVO.getReturnUrl());
			return parse2Mav(request, responseVO);
		}
		
		WebUtil.storeUserLoginRecorderInSession(request, user);
		if (requestVO.isShouldRemember()){
			WebUtil.storeUserProfileInCookie(response, user.getId());
		}else{
			WebUtil.removeUserProfileFromCookie(response);
		}
		
		responseVO.setExecuteResult(true);
		responseVO.setErrCode(BasicErrorCode._ACCESS_CODE_APPROVE);
		responseVO.setViewName("redirect:"+requestVO.getReturnUrl());
		
		return parse2Mav(request, responseVO);
	}
	
	@Override
	@RequestMapping("preLogin")
	public ModelAndView preLogin(HttpServletRequest request,HttpServletResponse response)
		throws Exception{
		
		
		boolean findValidUser=false;
		ResponseVO responseVO = (ResponseVO) JSONObject.toBean((JSONObject)request.getAttribute(BasicAppConstant._KEY_RESPONSE_VO),ResponseVO.class);
		
		String loginPageViewName = systemConfigService.findConfigByKey(BasicAppConstant._SYSCONF_KEY_LOGIN_PAGE).getValue();
		String returnUrl = (String) responseVO.getObjects().get(BasicAppConstant._KEY_LOGIN_RETURN_URL);
		
		
		Integer userIdInCookie = WebUtil.getUserIdFromCookie(request);
		
		BasicUserInfo userInfoInCookie = null;
		if (userIdInCookie!=null){
			userInfoInCookie = this.userInfoService.findById(userIdInCookie);
			if (userInfoInCookie!=null){
				WebUtil.storeUserLoginRecorderInSession(request, userInfoInCookie);
				findValidUser = true;
			}
		}
		

		if (findValidUser){
			responseVO.setExecuteResult(true);
			responseVO.setErrCode(BasicErrorCode._ACCESS_CODE_APPROVE);
			responseVO.setViewName("redirect:"+returnUrl);
		}else{
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(BasicErrorCode._ACCESS_CODE_NEED_LOGIN);
			responseVO.setViewName(loginPageViewName);
			
		}
		
		return parse2Mav(request, responseVO);
		
		
	}

	@Override
	@RequestMapping("logout")
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response,String returnUrl) {
		WebUtil.removeUserLoginRecorderInSession(request);
		WebUtil.removeUserProfileFromCookie(response);
		ResponseVO responseVO = new ResponseVO();
		responseVO.setViewName("redirect:"+returnUrl);
		return parse2Mav(request,responseVO);
	}
	
}
